#ifndef FRIENDSESSIONAREA_H
#define FRIENDSESSIONAREA_H

#include <QWidget>
#include <QScrollArea>
#include <QLabel>

// ------------------------------------------------------------ //
// --------------------- 滚动区域的Item类型 ------------------- //
// ------------------------------------------------------------ //
enum ItemType
{
    SessionItemType,
    FriendItemType,
    ApplyItemType
};

// ------------------------------------------------------------ //
// --------------------- 实现滚动区域 ------------------------- //
// ------------------------------------------------------------ //
class FriendSessionArea : public QScrollArea
{
    Q_OBJECT
public:
    explicit FriendSessionArea(QWidget* parent = nullptr);

    // 清空区域内item
    void clear();
    // 添加item
    // SessionItem -> sessionid
    // FriendItem、ApplyItem -> userid
    void addItem(const ItemType& type, const QString& id, const QIcon& avatar, const QString& name, const QString& text);

private:
    QWidget* container; // 修正拼写错误
};

// ------------------------------------------------------------ //
// --------------------- 会话基类 ------------------------- //
// ------------------------------------------------------------ //
class FriendSessionItem : public QWidget
{
    Q_OBJECT
public:
    // 使用const QString&以避免不必要的复制
    FriendSessionItem() = default;
    FriendSessionItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text);

    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void enterEvent(QEnterEvent* event) override;
    void leaveEvent(QEvent* event) override;

    void select();

    // 点击当前会话后的业务逻辑
    virtual void active();

private:
    // 当前会话是否被选中
    bool selected = false; // 初始化selected
    // 会话主体
    QWidget* owner;

protected: // 可以被子类访问
    QLabel* message_label;
};

// ------------------------------------------------------------ //
// --------------------- 会话Item ------------------------- //
// ------------------------------------------------------------ //
class SessionItem : public FriendSessionItem
{
    Q_OBJECT
public:
    SessionItem() = default;
    // 使用const QString&以避免不必要的复制
    SessionItem(QWidget* owner, const QString& sessionId, const QString& name, const QIcon& avatar, const QString& lastMessage);

    void active() override;

private:
    QString session_id;
    QString last_message;
};

// ------------------------------------------------------------ //
// --------------------- 好友Item ------------------------- //
// ------------------------------------------------------------ //
class FriendItem : public FriendSessionItem
{
    Q_OBJECT
public:
    FriendItem() = default;
    // 使用const QString&以避免不必要的复制
    FriendItem(QWidget* owner, const QString& userId, const QString& name, const QIcon& avatar, const QString& description);

    void active() override;

private:
    QString user_id;
};

// ------------------------------------------------------------ //
// --------------------- 申请Item ------------------------- //
// ------------------------------------------------------------ //
class ApplyItem : public FriendSessionItem
{
    Q_OBJECT
public:
    ApplyItem() = default;
    // 使用const QString&以避免不必要的复制
    ApplyItem(QWidget* owner, const QString& userId, const QString& name, const QIcon& avatar);

    void active() override;

    void acceptApply();
    void rejectApply();

private:
    QString user_id;
};

#endif // FRIENDSESSIONAREA_H
